/**
 * Copyright (c) 2011 - 2012, Sina Inc. All rights reserved.
 * @fileoverview Sina  表单-checkbox组件类
 * @author  bowen liu | liubowen082@163.com
 * @version 1.0 | 2012-2-23
 * @param
 * opt{
 * 	'el': ''       // 父容器标签
 * }
 * form_data="isMust=1&isChange=1"
 * @example
 * var a = new STK.cpt.extend.extCheckbox(opt);
 */
$require('form.formInputRc');
$require('form.cFormTips');
LJS.define('form.cFormCheckbox', function(ljs){
	var extCheckboxTpl = {
		tip: '请选择色系',
        error: '您至少选一个'
		}
    var extCheckbox = function(opt){
        var that = this;
        if (!opt || !opt.el) 
            return false;
        // 父容器标签
        that.el = opt.el;
        // 解form_data序列获得全局属性对象
        var _formData = that.el.getAttribute('form_data');
        that.data = typeof _formData === 'string' ? $.queryToJson(_formData) : {};
        // 是否是必选项
        that.isMust = parseInt(that.data.isMust) || 0;
        // 是否实时判断合法性
        that.isChange = parseInt(that.data.isChange) || 0;
        that.init();
    };
    // 继承formInputRc
    extCheckbox.prototype = new ljs.form.formInputRc;
    extCheckbox.prototype.init = function(){
        var that = this;
        that.tip = new ljs.form.cFormTips({
            'el': that.el
        });
        that.click({
            callback: function(spec){
                if (that.isChange) {
					if(!that.check())
						that.showError();
					else
						that.hide();
                    //console.info(that.check());
                } else {
                    //console.info(spec);
                }
            }
        });
    };
    /**
     * 检测
     * @return Boolean true || false
     * 当为非必填项时,直接返回true
     */
    extCheckbox.prototype.check = function(){
        var that = this;
        if (!that.isMust) {
            return true;
        } else {
            return that.getCheckedRc('checkbox').length > 0;
        }
    };
    // 显示提示层
    extCheckbox.prototype.showTip = function(){
        var that = this;
        that.tip.show(extCheckboxTpl.tip);
    };
    // 显示错误提示层
    extCheckbox.prototype.showError = function(){
        var that = this;
        that.tip.show(extCheckboxTpl.error,1);
    };
    // 隐藏层
    extCheckbox.prototype.hide = function(){
        var that = this;
        that.tip.hide();
    };
    /**
     * 更新html标签的form_data序列
     * 同时更新全局属性对象that.data
     */
    extCheckbox.prototype.J2Q = function(){
        var that = this;
        var opt = {
            'isMust': parseInt(that.isMust) || 0,
            'isChange': parseInt(that.isChange) || 0
        };
        that.data = $.jsonToQuery(opt);
        that.el.setAttribute('form_data', that.data);
    };
    return extCheckbox;
});
